package com.gyms.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.okhttp.AppChangeUtils;
import com.classic.okhttp.beans.HVHistoryUserInfoBean;
import com.classic.okhttp.beans.HVHistoryUsersBean;
import com.classic.okhttp.beans.HVProductInfoBean;
import com.classic.okhttp.beans.HVProjectInfoBean;
import com.classic.okhttp.beans.HVVenueInfoBean;
import com.classic.okhttp.beans.HVVenueLabelBean;
import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.services.IVenueService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.adapter.GymDetailImgAdapter;
import com.gyms.adapter.OlineCourseAdapter;
import com.gyms.animotion.CollectAnimotion;
import com.gyms.base.BaseActivity;
import com.gyms.constant.Constants;
import com.gyms.fragment.OrderFragment;
import com.gyms.manager.CommonDialogUtils;
import com.gyms.manager.DataCenter;
import com.gyms.manager.TitleManager;
import com.gyms.view.bottomview.ShareDialog;
import com.orhanobut.logger.Logger;
import com.umeng.socialize.UMShareAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import adapter.MyFragmentpagerAdapter;
import butterknife.BindView;
import butterknife.OnClick;
import constant.EventConstant;
import constant.PicSplit;
import glide.ImageLoaderUtils;
import myInterface.OnItemClickLitener;
import utils.ActivityUtils;
import utils.BarUtils;
import utils.ScreenUtils;
import utils.SizeUtils;
import utils.StringUtils;
import weight.ComDialog;
import weight.CustomViewPager;
import weight.ExpandableTextView;
import weight.FullyLinearLayoutManager;
import weight.HeadZoomScrollView;
import weight.MyListView;
import weight.UiEvent;
import weight.XCFlowLayout;


/**
 * 场馆详情
 * ltx
 */

public class GymDetailActivity extends BaseActivity implements OnItemClickLitener, AdapterView.OnItemClickListener, View.OnClickListener, HeadZoomScrollView.ScrollViewListener {
    @BindView(R.id.rl_tile)
    RelativeLayout mTitlelayout;
    @BindView(R.id.sv)//scrollView
    HeadZoomScrollView mSv;
    @BindView(R.id.rv_gym_img)//展示场馆图片list
    RecyclerView mRvGymImag;
    @BindView(R.id.tv_gym_phone)//电话Text
    TextView mTvGymPhone;
    @BindView(R.id.tv_gym_phone_big)//电话大的按钮
    TextView mTvGymhoneBig;
    private ExpandableTextView mTvNoticeMore;
    @BindView(R.id.tv_gym_intro)
    ExpandableTextView mTvGymIntro;
    @BindView(R.id.tl_order)//预定的tab
    TabLayout mTlOrder;
    @BindView(R.id.vp_gym_order)//Viewager
    CustomViewPager mVpGymOrder;
    @BindView(R.id.tv_no_true)
    TextView mTvNoTrue;
    @BindView(R.id.iv_detail_ng)
    ImageView mIvDetailNg;
    @BindView(R.id.tv_gym_name)
    TextView mTvGymName;
    @BindView(R.id.tv_gym_adress)
    TextView mTvGymAdress;
    @BindView(R.id.ll_gym_adress)
    LinearLayout mLlGymAdress;
    @BindView(R.id.tv_service_time)
    TextView mTvServiceTime;//营业时间View
    @BindView(R.id.ll_lables)
    XCFlowLayout mLlLables;
    @BindView(R.id.ll_gym_intro)
    LinearLayout mLlGymIntro;//场馆介绍
    @BindView(R.id.ll_gym_notice)
    LinearLayout mLLGymNotice;
    @BindView(R.id.ll_order_gym)
    LinearLayout mLLOrderGym;//场馆详情
    @BindView(R.id.mv_online_course)
    MyListView mMvOnlineCourse;//在售课程
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_online_course)
    TextView mTvOnlineCourse;
    @BindView(R.id.iv_collect)
    ImageView mIvCollect;
    @BindView(R.id.iv_gym_share)
    ImageView mIvGymShare;
    @BindView(R.id.view_tile)
    View mViewTitle;
    @BindView(R.id.statu_bar)
    ImageView mStatuBar;
    private int height;//相对的参考高度

    /*拨打电话*/
    private Intent mPhoneIntent;
    private String gymPhone;

    /*预定的tab*/
    //标题list
    private List<Fragment> mOrderFragments = new ArrayList<>();

    /*展示场馆图片*/
    private GymDetailImgAdapter gymDetailImgAdapter;
    /*展示页面数据相关*/
    public String venueId;//场馆Id
    private HVVenueInfoBean mVVenueInfoBean;

    /*在售课程*/
    private OlineCourseAdapter mOlineCourseAdapter;

    /*场馆项目名称*/
    private List<String> mProjectNames;
    /*//标题栏渐变
    private TextView mMidleView;*/
    private boolean isCollect;//是否已经收藏
    private boolean isShowBlue;
    //电话预定
    @BindView(R.id.ll_phone_order)
    LinearLayout mLLPhoneOrder;
    @BindView(R.id.ll_notice_out)
    LinearLayout mLLNoticeOut;
    @BindView(R.id.view_notice_line)
    View mViewNoticeLine;//场馆公告上面的线
    @BindView(R.id.tv_gym_photo)
    TextView mTvGymPhoto;//场馆图片
   @BindView(R.id.view_online_course)
    View mOnlineCourse;//在售课程
    private List<ImageView> images = new ArrayList<>();//tab的imgview集合
    private Animation animation;
    private String mTitle;//标题

    //购买记录 相关
    @BindView(R.id.ll_history_buy_layout)
    LinearLayout mLlBuyHistoryLayoyt;//购买记录的linearLayout
    @BindView(R.id.ll_buy_history)
    LinearLayout mLlBuyHistory;//购买记录的linearLayout
    @BindView(R.id.tv_buy_num)
    TextView mTvBuyNum;//购买记录的人数

    private int minDis=10;//最小间距 10dp
    private int mCountView=80;//显示数量的大小为70dp
    private int singleHead=48;//单个头像的大小 35dp
    private int actDis;//实际间距
    private int mCount;//可存放头像的大小
    private int mPhoneWidth;//屏幕宽度
    private int mPaddingLeft=15;//paddingleft的值是15

    private  String mShortAdress;
    //场馆服务
    @BindView(R.id.ll_gym_service)
    LinearLayout mLLGymService;
    //营业时间布局
    @BindView(R.id.ll_service_time)
    LinearLayout mLlServiceTime;

    private ShareDialog mShareDialog;

    private CollectAnimotion mCollectAnimotion;//收藏的动画相关类

    private android.os.Handler mHandler=new android.os.Handler();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_gym_detail;
    }

    @Override
    protected void initView() {
        initData();
        BarUtils.setStatuBar(mContext,mStatuBar);
        createLoadingView("加载中", true);
        Bundle mBundle = getIntent().getExtras();
        //如果是呼啦场馆通隐藏分享按钮
        if(!AppChangeUtils.isGymApp){
            mIvGymShare.setVisibility(View.GONE);
        }
        if (null != mBundle) {
            venueId = mBundle.getString(Constants.FLAG_INTENT_VENUEID, "");
        }
        LinearLayoutManager imgLinearLayoutManager = new FullyLinearLayoutManager(this);
        imgLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvGymImag.setLayoutManager(imgLinearLayoutManager);
        gymDetailImgAdapter = new GymDetailImgAdapter(this);
        mRvGymImag.setAdapter(gymDetailImgAdapter);
        mPhoneIntent = new Intent(Intent.ACTION_DIAL);

        initOnlineCouseList();
        mMvOnlineCourse.setFocusable(false);//解决默认不置顶
        mVpGymOrder.setFocusable(false);//解决默认不置顶
        mRvGymImag.setFocusable(false);//解决默认不置顶
        mTvNoticeMore = (ExpandableTextView) findViewById(R.id.expand_text_view);
        animation = AnimationUtils.loadAnimation(this,R.anim.applaud_animation);

        mPhoneWidth=ScreenUtils.getScreenWidth(this);

        mSv.setZoomView(mIvDetailNg);
    }

    private void initData() {
        mCollectAnimotion=new CollectAnimotion(mHandler,mIvCollect);
    }

    @Override
    protected void setTitleTheme() {
        ScreenUtils.translateStatusBar(GymDetailActivity.this);
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    protected void setListener() {
        //注册EventBus
        EventBus.getDefault().register(this);
        /**
         * title透明显示相关
         */
        ViewTreeObserver vto = mIvDetailNg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mTitlelayout.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = mIvDetailNg.getHeight();
            }
        });
        mSv.setScrollViewListener(this);

        gymDetailImgAdapter.setOnItemClickLitener(this);//场馆图片Item点击事件
        mMvOnlineCourse.setOnItemClickListener(this);
        mLlGymAdress.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
              /*  final ComDialog dialog = CommonDialogUtils.dialog(GymDetailActivity.this,getSupportFragmentManager(),mTvGymAdress.getText().toString());
                dialog.setSubmitText("复制到剪贴板");
                dialog.setSubmit(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {*/
                        // 从API11开始android推荐使用android.content.ClipboardManager
                        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        // 将文本内容放到系统剪贴板里。
                        cm.setText(mTvGymAdress.getText().toString());
                        ToastUtil.showToast(mContext, "复制成功");
                        /* dialog.dismiss();
                   }
                });*/
                return true;
            }
        });
    }

    @Override
    protected void processLogic() {
//        ScreenUtils.translateStatusBar(this);
        getVenueInfo(false);
    }

    /**
     * 初始化在售课程列表
     */
    private void initOnlineCouseList() {
        mOlineCourseAdapter = new OlineCourseAdapter(mContext);
        mMvOnlineCourse.setAdapter(mOlineCourseAdapter);
    }

    /**
     * 获取场馆详情的数据
     * @param isRefresh
     */
    private void getVenueInfo( final boolean isRefresh) {
        loadingView.show();
        IVenueService.getVenueInfo(this, venueId, true, new ObjectCallback<HVVenueInfoBean>() {
            @Override
            public void onSuccess(HVVenueInfoBean response, String message) {
                if (loadingView != null) {
                    loadingView.dismiss();
                }
                if (null != response) {

                    mVVenueInfoBean = response;
                    if(!isRefresh){
                        doFullView();
                    }else{
                        //收藏初始化
                        isCollect = mVVenueInfoBean.getIsCollect();
                        changeCollectStatue(isCollect,isRefresh);
                    }
                }else{//response为null 就finish  代表下架
                    ToastUtil.showToast(mContext,"该场馆不存在或已下架");
                    finish();
                    return;
                }
            }

            @Override
            public void onErrorT(int code, HVVenueInfoBean response, String errorMessage) {
                if (null != response) {//缓存
                    mVVenueInfoBean = response;
                    doFullView();
                }
                if (loadingView != null) {
                    loadingView.dismiss();
                }
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                if (loadingView != null) {
                    loadingView.dismiss();
                }
            }
        });

    }

    /**
     * 拿到数据填充
     */

    private void doFullView() {
        mTitle=mVVenueInfoBean.getVenueName();
        mShortAdress=mVVenueInfoBean.getShortAddress();
        ImageLoaderUtils.display(getApplicationContext(), mIvDetailNg, mVVenueInfoBean.getVenueUrl());
        //收藏初始化
        isCollect = mVVenueInfoBean.getIsCollect();
        changeCollectStatue(isCollect, false);

        mTvGymName.setText(mVVenueInfoBean.getVenueName());
        mTvGymAdress.setText(mVVenueInfoBean.getAddress());
        if (StringUtils.isNull(mVVenueInfoBean.getAnnouncement())) {//公告为空隐藏
            mLLGymNotice.setVisibility(View.GONE);
            mLLNoticeOut.setVisibility(View.GONE);
            mViewNoticeLine.setVisibility(View.GONE);//隐藏下划线
        } else{
            mViewNoticeLine.setVisibility(View.VISIBLE);//显示下划线
            mTvNoticeMore.setText(mVVenueInfoBean.getAnnouncement());
        }
        mTvGymhoneBig.setText(mVVenueInfoBean.getPhone());
        if (StringUtils.isNull(mVVenueInfoBean.getIntroduce())) {//场馆介绍为空隐藏
            mLlGymIntro.setVisibility(View.GONE);
        } else
            mTvGymIntro.setText(mVVenueInfoBean.getIntroduce());
        String mOpenTime=mVVenueInfoBean.getOpenTime();//营业时间
        mTvServiceTime.setText(mOpenTime);
        gymPhone = mVVenueInfoBean.getPhone();
        mLlServiceTime.setVisibility(StringUtils.isEmpty(mOpenTime) ? View.GONE : View.VISIBLE);
        List<HVVenueLabelBean> mHVVenueLabelBeans = mVVenueInfoBean.getVenueLabel();
        showLable(mHVVenueLabelBeans);
        if(StringUtils.isNull(mOpenTime)&&StringUtils.isNull(mHVVenueLabelBeans)){
            mLLGymService.setVisibility(View.GONE);
        }
        //场馆图片
        gymDetailImgAdapter.setVenuePic(mVVenueInfoBean.getVenuePic());
        gymDetailImgAdapter.notifyDataSetChanged();
        if(StringUtils.isNull(mVVenueInfoBean.getVenuePic())){//场馆图片为null就隐藏标签
            mTvGymPhoto.setVisibility(View.GONE);
            mRvGymImag.setVisibility(View.GONE);
        }
        //在售课程
        mOlineCourseAdapter.setProducts(mVVenueInfoBean.getProducts());

        if (StringUtils.isNull(mVVenueInfoBean.getProducts())) {//在售课程
            mMvOnlineCourse.setVisibility(View.GONE);
            mTvOnlineCourse.setVisibility(View.GONE);
            mOnlineCourse.setVisibility(View.GONE);//在售课程的空白View
        }
        mOlineCourseAdapter.notifyDataSetChanged();
        //场馆预定
        mProjectNames = getProjectNames();
        if (StringUtils.isNull(mVVenueInfoBean.getProjectInfo())) {//馆馆预定为null 就隐藏
            mLLOrderGym.setVisibility(View.GONE);//无预定数据隐藏
        }
        if(StringUtils.isNull(mVVenueInfoBean.getProjectInfo())&&StringUtils.isNull(mVVenueInfoBean.getProducts())){//馆馆预定并且产品为null的时候出现电话预定模块
            mLLPhoneOrder.setVisibility(View.VISIBLE);
        }else{
            mLLPhoneOrder.setVisibility(View.GONE);
        }

        initOrderTab();
        initOrderViewpager();
        //购买历史记录列表
        HVHistoryUsersBean mHistoryUsers=mVVenueInfoBean.getHistoryUsers();
        if (!StringUtils.isNull(mHistoryUsers)) {
            initBuyHistory(mHistoryUsers);
        } else {
            mLlBuyHistoryLayoyt.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化购买历史记录
     * @param mHistoryUsers 历史购买bean
     */

    private void initBuyHistory(HVHistoryUsersBean mHistoryUsers) {
        ArrayList<HVHistoryUserInfoBean> users=mHistoryUsers.getUsers();//用户列表
        if(StringUtils.isNull(users)){
            mLlBuyHistoryLayoyt.setVisibility(View.GONE);
            return;
        }
        int mBuyNum=mHistoryUsers.getHistoryUserNumber();//购买人数
        countNum();
        countActDis();

        int mUserSize=users.size();
        mCount=mCount>=mUserSize?mUserSize:mCount;//摆的个数 不能超出 users的个数
        for(int pos=0;pos<mCount;pos++){
            HVHistoryUserInfoBean mBuyUserInfo=users.get(pos);//购买的用户的信息
            doCreateView(mBuyUserInfo,pos);
        }
        mTvBuyNum.setText(mBuyNum+"人");

    }

    /**
     * 创建子view
     * @param mUserInfo 当前购买的历史信息
     * @param pos 当前位置
     */

    private void doCreateView(HVHistoryUserInfoBean mUserInfo, int pos) {
        View mChildView= LayoutInflater.from(mContext).inflate(R.layout.buy_history_layout,null);
        LinearLayout.LayoutParams mLpBuyLayout = new LinearLayout.LayoutParams(SizeUtils.dp2px(mContext,singleHead), ViewGroup.LayoutParams.WRAP_CONTENT);//包含头像的layout变量
        if(pos!=0){
            mLpBuyLayout.setMargins(actDis,0,0,0);
        }
        LinearLayout.LayoutParams mLpHead = new LinearLayout.LayoutParams(SizeUtils.dp2px(mContext,singleHead),SizeUtils.dp2px(mContext,singleHead));//头像的布局变量
        mChildView.setLayoutParams(mLpBuyLayout);
        TextView mTvBuyName= (TextView) mChildView.findViewById(R.id.tv_buy_name);
        ImageView mBuyHead= (ImageView) mChildView.findViewById(R.id.iv_buy_head);
        String mHeadUrl=mUserInfo.getImageUrl();
        String mBuyName=mUserInfo.getUserName();
        mTvBuyName.setText(mBuyName);
        mBuyHead.setLayoutParams(mLpHead);
        ImageLoaderUtils.loadingCircle(mContext,mHeadUrl,mBuyHead);
        mLlBuyHistory.addView(mChildView);
    }

    /**
     * 计算真实的间距
     */

    private void countActDis() {
        actDis=(mPhoneWidth-SizeUtils.dp2px(mContext,mPaddingLeft)-SizeUtils.dp2px(mContext,mCountView)-mCount*SizeUtils.dp2px(mContext,singleHead))/mCount;
        Logger.e("actDis--->"+actDis);
    }

    /**
     * 根据最小的间距来计算个数
     */

    private void countNum() {
        mCount=(mPhoneWidth-SizeUtils.dp2px(mContext,mPaddingLeft)-SizeUtils.dp2px(mContext,mCountView))/SizeUtils.dp2px(mContext,(singleHead+minDis));
        Logger.e("mCount--->"+mCount);
    }

    /**
     * 获取场馆项目的名称
     */

    private List<String> getProjectNames() {
        List<String> mProjectNames = new ArrayList<>();
        List<HVProjectInfoBean> mHVProjectInfoBeans = mVVenueInfoBean.getProjectInfo();
        for (HVProjectInfoBean mHVProjectInfoBean : mHVProjectInfoBeans) {
            mProjectNames.add(mHVProjectInfoBean.getProjectName());
        }
        return mProjectNames;
    }

    /**
     * 显示标签
     *
     * @param mHVVenueLabelBeans 标签的类
     */
    private void showLable(List<HVVenueLabelBean> mHVVenueLabelBeans) {
        if (null == mHVVenueLabelBeans || mHVVenueLabelBeans.size() == 0) {
            mLlLables.setVisibility(View.GONE);
        } else {
            doShowUrlImg(mHVVenueLabelBeans);
        }
    }

    /**
     * 根据具体的网络尺寸加载
     *
     * @param mHVVenueLabelBeans 标签的数据集
     */

    private void doShowUrlImg(final List<HVVenueLabelBean> mHVVenueLabelBeans) {
        if (StringUtils.isNull(mHVVenueLabelBeans)) {
            return;
        }
        for (int pos = 0; pos < mHVVenueLabelBeans.size(); pos++) {
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(mContext, 15));
            ImageView mIv = new ImageView(mContext);
            if (pos == 0) {
                ll.setMargins(0, SizeUtils.dp2px(mContext, 5), 0, 0);
            }else{
                ll.setMargins(SizeUtils.dp2px(mContext, 5), SizeUtils.dp2px(mContext, 5), 0, 0);

            }
            mIv.setLayoutParams(ll);
            Glide.with(mContext).load(mHVVenueLabelBeans.get(pos).getDiscountIcon()).fitCenter().into(mIv);
            mLlLables.addView(mIv);
        }
    }

    @OnClick({R.id.tv_gym_phone, R.id.tv_gym_phone_big, R.id.ll_gym_adress,R.id.iv_gym_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_gym_phone:
            case R.id.tv_gym_phone_big://拨打电话
                final ComDialog dialog = CommonDialogUtils.dialog(GymDetailActivity.this,getSupportFragmentManager(),getString(R.string.str_call),AppChangeUtils.PHONE_TITLE, getString(R.string.str_call), "取消");
                dialog.setSubmit(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Uri data = Uri.parse("tel:" + gymPhone);
                        mPhoneIntent.setData(data);
                        startActivity(mPhoneIntent);
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.ll_gym_adress://跳转到地图
                if (StringUtils.isNull(mVVenueInfoBean)) {
                    return;
                }
                Intent intent = new Intent(mContext, LocationMapActivity.class);
                intent.putExtra(Constants.FLAG_INTENT_LATITUDE, mVVenueInfoBean.getLatitude());//经度
                intent.putExtra(Constants.FLAG_INTENT_LONGITUDE, mVVenueInfoBean.getLongitude());//维度
                intent.putExtra(Constants.FLAG_INTENT_ADDRESS, mShortAdress);//地址
                intent.putExtra(Constants.FLAG_INTENT_GYM_NAME, mVVenueInfoBean.getVenueName());//场馆名称
                startActivity(intent,false);
                break;

            case R.id.iv_gym_share:// 分享
                if (null == mShareDialog) {
                    mShareDialog = new ShareDialog(this, R.style.BottomToTopAnim);
                    mShareDialog.setTitle(PicSplit.makeShareTitle(mTitle));
                    String mShareGymUrl= PicSplit.makeShareUrl(PicSplit.S_VENUE_ID,venueId,null,null);
                    mShareDialog.setTagUrl(mShareGymUrl);

                    mShareDialog.setShareContent(PicSplit.makeSHareContent(mShortAdress));
                }
                mShareDialog.show();

                break;
        }
    }

    /**
     * 初始化预定的tab选中栏
     */

    private void initOrderTab() {
        if (null == mProjectNames || mProjectNames.size() == 0) {
            return;
        }
        //tablayout 赋值
        for (int i = 0; i < mProjectNames.size(); i++) {//给tablayout赋值
            mTlOrder.addTab(mTlOrder.newTab().setText(mProjectNames.get(i)));
        }
        //创建Fragment页面
        for (int i = 0; i < mProjectNames.size(); i++) {//创建fragment
            OrderFragment f1 = new OrderFragment();
            Bundle mBundle = new Bundle();
            mBundle.putSerializable(Constants.FLAG_INTENT_PROJECT, mVVenueInfoBean.getProjectInfo().get(i));
            f1.setArguments(mBundle);
            mOrderFragments.add(f1);
        }
    }


    @Override
    public void onItemClick(View view, int position) {
        Bundle mBudle = new Bundle();
        mBudle.putInt(Constants.FLAG_INTENT_PAGERNUMBER, position);
        mBudle.putSerializable(Constants.FLAG_INTENT_VENUEPIC, mVVenueInfoBean.getVenuePic());
        ActivityUtils.makeScaleLaunch(this,view,mBudle,ShowImagActivity.class);
    }

    /**
     * 将ViewPager和Tablayout相关联
     */

    private void initOrderViewpager() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        mVpGymOrder.setAdapter(new MyFragmentpagerAdapter(supportFragmentManager, mProjectNames, mOrderFragments));
        mTlOrder.setupWithViewPager(mVpGymOrder);//将TabLayout和ViewPager关联起来。
    }


    @OnClick(R.id.tv_no_true)
    public void doReport() {
        if (!DataCenter.getInstance().checkIsLogin()) {
            startActivity(CodeLoginActivity.class, false);
        }else {
            Intent intent = new Intent(mContext, ReportActivity.class);
            intent.putExtra(Constants.FLAG_INTENT_VENUEID, venueId);
            startActivity(intent, false);
        }
    }


    @OnClick({R.id.iv_back, R.id.iv_collect})
    public void onClickTitle(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.iv_collect://收藏
                sartActivityIsLogin();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mMvOnlineCourse) {//在线课程
            Intent intent = new Intent(mContext, ProductDetailsActivity.class);
            ArrayList<HVProductInfoBean> mVVenueInfoBeanProducts = mVVenueInfoBean.getProducts();
            String mProductId = mVVenueInfoBeanProducts.get(position).getProductId();
            intent.putExtra(Constants.FALG_INTENT_PRPDUCT_ID, mProductId);
            startActivity(intent);
        }
    }

    /**
     * 是否登录判断跳转
     */
    public void sartActivityIsLogin() {
        if (!DataCenter.getInstance().checkIsLogin()) {
            ToastUtil.showToast(mContext, "请登录后重试");
            startActivity(CodeLoginActivity.class, false);
        } else {
            if (!StringUtils.isNull(mVVenueInfoBean)) {
                if (mVVenueInfoBean.getIsCollect()) {//
                    cancelMyCollection();
                } else {//收藏
                    collect();
                }
            }
        }
    }

    /**
     * 收藏
     */
    private void collect() {
        loadingView.show();
        IUserService.collect(this, mVVenueInfoBean.getVenueId(), Enum.EnumCollect.Venue, false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                mCollectAnimotion.stopAnimotion();
                mVVenueInfoBean.setIsCollect(true);
                isCollect = true;
                changeCollectStatue(true, false);
                mIvCollect.startAnimation(animation);
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_REFRESH_MY_VENUE, null));
                ToastUtil.showToast(mContext, message);
                dialogDis();
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    /**
     * 取消收藏
     */

    private void cancelMyCollection() {
        loadingView.show();
        IUserService.cancelMyCollection(this, Enum.EnumCollect.Venue, mVVenueInfoBean.getVenueId(), false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                mVVenueInfoBean.setIsCollect(false);
                isCollect = false;
                mCollectAnimotion.startAnimotion();
                changeCollectStatue(isCollect, false);
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_REFRESH_MY_VENUE, null));
                ToastUtil.showToast(mContext, message);
                dialogDis();
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    public void changeCollectStatue(boolean isCollect, boolean isRefresh) {
        if(isCollect){
            mIvCollect.setImageResource(isShowBlue?R.mipmap.icon_star_press:R.mipmap.icon_shoucang_yixuan);
            mCollectAnimotion.stopAnimotion();
        }else{
            if(isRefresh)//已经启动动画就不需要再启动
                return;
            mIvCollect.setImageResource(isShowBlue?R.mipmap.icon_starblue_normal:R.mipmap.icon_star_normal);
            mCollectAnimotion.startAnimotion();
        }
    }

    /**
     * EventBus 接收事件
     * 刷新我的信息
     *
     */
    @Subscribe
    public void toReflashInfo(UiEvent uiEvent) {
        if (uiEvent.getKey().equals(EventConstant.S_MYINFO)) {
            getVenueInfo(true);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//销毁的时候注销EventBus
        UMShareAPI.get(this).release();
        mCollectAnimotion.stopAnimotion();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onScrollChanged(HeadZoomScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            mTitlelayout.setBackgroundColor(Color.argb(0, 250, 250, 250));
            mIvBack.setImageResource(R.drawable.close_w_selector);
            mIvGymShare.setImageResource(R.mipmap.icon_fenxiang_w);
            if (!isCollect) {//显示白色收藏
                mIvCollect.setImageResource(R.mipmap.icon_star_normal);
            }else {
                mIvCollect.setImageResource(R.mipmap.icon_shoucang_yixuan);
            }
            mViewTitle.setVisibility(View.GONE);
//            mTvTitle.setTextColor(Color.argb(255, 255, 255, 255));
            mTvTitle.setText("");
            isShowBlue = false;//在透明的背景下设置成白色

        } else if (y > 0 && y <= height / 2) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / (height / 2);
            float alpha = (255 * scale);
//            mTvTitle.setTextColor(Color.argb((int) alpha, 51, 51, 51));
            mTvTitle.setText("");
            mIvBack.setImageResource(R.drawable.close_w_selector);
            if (!isCollect) {
                mIvCollect.setImageResource(R.mipmap.icon_star_normal);
            }else {
                mIvCollect.setImageResource(R.mipmap.icon_shoucang_yixuan);
            }
            mIvGymShare.setImageResource(R.mipmap.icon_fenxiang_w);
            mTitlelayout.setBackgroundColor(Color.argb((int) alpha, 250, 250, 250));
            mViewTitle.setVisibility(View.GONE);
            isShowBlue = false;//在透明的背景下设置成白色
        } else {    //滑动到banner下面设置普通颜色
            mTitlelayout.setBackgroundColor(Color.argb(255, 250, 250, 250));
            if (!isCollect) {
                mIvCollect.setImageResource(R.mipmap.icon_starblue_normal);
            }else {
                mIvCollect.setImageResource(R.mipmap.icon_star_press);
            }
            mIvGymShare.setImageResource(R.mipmap.icon_fenxiang);
            mIvBack.setImageResource(R.drawable.close_b_selector);
            mViewTitle.setVisibility(View.VISIBLE);
            mTvTitle.setTextColor(Color.argb(255, 51, 51, 51));
            mTvTitle.setText(mTitle);
            isShowBlue = true;//渐变白色后需要设置成蓝色

        }
    }
}
